﻿//
// Created by 17616 on 2021/5/22.
//

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <thread>
#include "public.h"

std::string in_pic = R"(C:\Users\17616\Desktop\本科毕设\实验数据记录\2021-5-20\G0_0-F4-0520\G0_0-LD3I2\合成.tiff)";
std::string back_pic = R"(C:\Users\17616\Desktop\本科毕设\实验数据记录\2021-5-20\G0_0-F4-0520\G0_0-LD3I2\back.tiff)";
std::string result_pic = R"(C:\Users\17616\Desktop\本科毕设\实验数据记录\2021-5-20\G0_0-F4-0520\G0_0-LD3I2\LASNR.tiff)";
std::string noise_pic = R"(C:\Users\17616\Desktop\本科毕设\实验数据记录\2021-5-20\G0_0-F4-0520\G0_0-LD3I2\noise.tiff)";

int main(int argn, const char **argv) {
    if (argn == 1) {
        return 0;
    } else if (strcmp(argv[1], "--test") == 0) {
    } else {
        std::cout << "file= " << argv[1] << std::endl;
        in_pic = argv[1];
    }
    cv::Mat in = cv::imread(in_pic, cv::IMREAD_UNCHANGED);
    cv::Mat back = cv::Mat(in.size(), CV_32F);
    cv::Mat in_sq = cv::Mat(in.size(), CV_32F);
    cv::Mat result = cv::Mat(in.size(), CV_32F);
    cv::Mat noise = cv::Mat(in.size(), CV_32F);
    int Sigma1, Sigma2;
    std::cout << "输入Sigma1 和Sigma2" << std::endl;
    std::cin >> Sigma1 >> Sigma2;
    Sigma1 |= 1;
    Sigma2 |= 1;
    std::cout << "Sigma1=" << Sigma1 << " Sigma2=" << Sigma2 << std::endl;
    std::cout << "计算......" << std::endl;
    //计算分母的矩阵，先平方，再高斯
    thread_pool tp(0, in.rows);
    tp.post([&](int start, int end) {
        float *fp_in;
        float *fp_in_sq;
        for (int r = start; r < end; ++r) {
            fp_in = (float *) ((size_t) in.data + r * in.step);
            fp_in_sq = (float *) ((size_t) in_sq.data + r * in_sq.step);
            for (int c = 0; c < in.cols; ++c) {
                *fp_in_sq = (*fp_in) * (*fp_in);
                fp_in_sq++;
                fp_in++;
            }
        }
    });
    tp.join();
    std::thread td1([&]() {
        cv::GaussianBlur(in_sq, noise, cv::Size(0, 0), Sigma2);
//        cv::imwrite(noise_pic, noise);
    });

    //计算分子，先高斯再，相减，再计算除法
    std::thread td2([&]() {
        cv::GaussianBlur(in, back, cv::Size(0, 0), Sigma1);
//        cv::imwrite(back_pic, back);
    });
    td1.join();
    td2.join();

    thread_pool tp2(0, in.rows);
    tp2.post([&](int start, int end) {
        float *fp_result;
        float *fp_noise;
        float *fp_in;
        float *fp_back;
        double buf;
        for (int r = start; r < end; ++r) {
            fp_result = (float *) ((size_t) result.data + r * result.step);
            fp_noise = (float *) ((size_t) noise.data + r * noise.step);
            fp_in = (float *) ((size_t) in.data + r * in.step);
            fp_back = (float *) ((size_t) back.data + r * back.step);
            for (int c = 0; c < in.cols; ++c) {
                buf = (*fp_in) - (*fp_back);//原图-高斯滤波，高通
//                buf *= 1e9;
                if(buf>0){
                    buf *= buf;
                }else{
                    buf = 0;
                }
                buf /= (*fp_noise); //除分母
//                *fp_result = buf == buf ? buf : 0;
                *fp_result = buf;
                fp_result++;
                fp_noise++;
                fp_in++;
                fp_back++;
            }
        }
    });

    tp2.join();
    cv::imwrite(GetFilePath(in_pic) + "lasnr.tiff", result);
    return 0;
}